package com.hh.TreeQuestions;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author hanghao
 * @Date 2023/7/3 14:56
 * @Version 1.0
 * 完全二叉树的节点个数
 * middle
 */

public class No222 {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        int res = 0;
        while (!deque.isEmpty()) {
            int len = deque.size();
            while (len > 0) {
                TreeNode node = deque.poll();
                res++;
                if(node.left != null) deque.offer(node.left);
                if(node.right != null) deque.offer(node.right);
                len--;
            }
        }
        return res;
    }
}
